{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Configuration of 5G RAN of Open Air Interface (OAI) with multiple gNBs and multiple UEs\n",
    "## Objectives\n",
    "- Configure multiple OAI gNBs and UEs\n",
    "\n",
    "![multiple_gNB_config_](../FIGS/H05_Multiple_gNB_and_UE/esquema_cenario_multi_gNB_and_multi_UE.drawio.png)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Linux namespace configuration\n",
    "\n",
    "To connect multiple gNBs and UEs, you'll need to set up a network namespace in Linux, as well as add route configurations, by following the commands below:\n",
    "\n",
    "**Step 1:** Creating a namespace:\n",
    "\n",
    "sudo ip netns add oai-2\n",
    "\n",
    "**Step 2:** Creating a pair of virtual interfaces (veth):\n",
    "\n",
    "sudo ip link add veth0 type veth peer name veth1\n",
    "\n",
    "**Step 3:** Moving one interface to the created namespace:\n",
    "\n",
    "sudo ip link set veth1 netns oai-2\n",
    "\n",
    "**Step 4:** Configuring an IP address for veth0 interface:\n",
    "\n",
    "sudo ip addr add 192.168.1.1/24 dev veth0\n",
    "\n",
    "**Step 5:** Configuring an IP address for veth1 interface within the namespace:\n",
    "\n",
    "sudo ip netns exec oai-2 ip addr add 192.168.1.2/24 dev veth1\n",
    "\n",
    "**Step 6:** Bringing up the veth0 interface:\n",
    "\n",
    "sudo ip link set veth0 up\n",
    "\n",
    "**Step 7:** Bringing up the veth1 interface within the namespace:\n",
    "\n",
    "sudo ip netns exec oai-2 ip link set veth1 up\n",
    "\n",
    "**Step 8:** Adding a route within the namespace:\n",
    "\n",
    "sudo ip netns exec oai-2 ip route add 192.168.70.128/26 via 192.168.1.1 dev veth1\n",
    "\n",
    "**Step 9:** Enabling packet forwarding on the system:\n",
    "\n",
    "sudo sysctl net.ipv4.conf.all.forwarding=1\n",
    "\n",
    "**Step 10:** Setting default policy for packet forwarding to ACCEPT:\n",
    "\n",
    "sudo iptables -P FORWARD ACCEPT\n",
    "\n",
    "**Step 11:** Enabling packet forwarding within the namespace:\n",
    "\n",
    "sudo ip netns exec oai-2 sysctl net.ipv4.conf.all.forwarding=1\n",
    "\n",
    "**Step 12:** Setting default policy for packet forwarding within the namespace to ACCEPT::\n",
    "\n",
    "sudo ip netns exec oai-2 iptables -P FORWARD ACCEPT"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Multiple gNB Configuration in the monolithic mode\n",
    "\n",
    "To connect multiple gNBs, follow the steps below in a new Linux Terminal:\n",
    "\n",
    "**Step 1:** Make a copy of the configuration file used to connect the first gNB: \n",
    "\n",
    "cd /home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF\n",
    "\n",
    "sudo cp gnb.sa.band78.fr1.106PRB.usrpb210.conf ./gnb2.sa.band78.fr1.106PRB.usrpb210.conf\n",
    "\n",
    "**Attention:** You must change the file name, in the example above \"gnb2.sa.band78.fr1.106PRB.usrpb210.conf\".\n",
    "\n",
    "**Step 2:**  Change gNB parameters\n",
    "\n",
    "This part you will change the gNB name and ID in (/home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb2.sa.band78.fr1.106PRB.usrpb210.conf), such as in figure below.\n",
    "\n",
    "![multiple_gNB_config_](../FIGS/H02_5GRAN_UNI_III/multiple_gNB_config.png)\n",
    "\n",
    "**Step 3:** Chenge gNB network interfaces:\n",
    "\n",
    "In the same file as in Step 2, change the network interfaces according to the namespace you created earlier, such as in figure below.\n",
    "\n",
    "![multiple_gNB_config_](../FIGS/H05_Multiple_gNB_and_UE/multiple_gnb_2.png)\n",
    "\n",
    "Open file by typing:\n",
    "\n",
    "gedit /home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb2.sa.band78.fr1.106PRB.usrpb210.conf\n",
    "\n",
    "**Attention:** The name in \"Active_gNBs\" has to be the same as the parameter \"gNB_name\", in the example of the figure above \"gNB2-OAI\".\n",
    "\n",
    "**Attention:** To connect other gNBs, simply repeat the steps, always entering different names and IDs for each gNB."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Multiple gNB and UE deployment\n",
    "\n",
    "To connect two UEs, one in each gNB, it will be necessary to deploy each \"UE <-> gNB\" pair separately, with one UE and its respective gNB inside the namespace created in the previous step, and the other pair outside the namespace.\n",
    "\n",
    "**Step 1:** Deployment of first UE and first gNB inside the namespace oai-2:\n",
    "\n",
    "***Step 1.1:*** Deployment of gNB:\n",
    "\n",
    "First, in a Linux terminal, go into the namespace you created:\n",
    "\n",
    "sudo ip netns exec oai-2 bash\n",
    "\n",
    "Next, to deploy the gNB, type in terminal:\n",
    "\n",
    "cd /home/dco1020/openairinterface5g/cmake_targets/ran_build/build/\n",
    "\n",
    "sudo RFSIMULATOR=server ./nr-softmodem --rfsim --sa -O ../../../targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb2.sa.band78.fr1.106PRB.usrpb210.conf --gNBs.[0].min_rxtxtime 6 --continuous-tx\n",
    "\n",
    "***Step 1.2:*** Deployment of UE:\n",
    "\n",
    "First, in another Linux terminal, go into the namespace you created:\n",
    "\n",
    "sudo ip netns exec oai-2 bash\n",
    "\n",
    "Next, to deploy the UE, type in terminal:\n",
    "\n",
    "cd /home/dco1020/openairinterface5g/cmake_targets/ran_build/build/\n",
    "\n",
    "sudo RFSIMULATOR=192.168.1.2 ./nr-uesoftmodem -r 106 --numerology 1 --band 78 -C 3619200000 --rfsim --sa --nokrnmod -O ../../../targets/PROJECTS/GENERIC-NR-5GC/CONF/ue.conf --uicc0.imsi 001010000000001\n",
    "\n",
    "**Attention:** You must execute the command above with the \"RFSIMULATOR\" parameter with the IP used in the gNB interface configuration step  \n",
    "\n",
    "**Step 2:** Deployment of second UE and second gNB outside the namespace oai-2:\n",
    "\n",
    "***Step 2.1:*** Deployment of gNB:\n",
    "\n",
    "To deploy the gNB, type in a new Linux terminal:\n",
    "\n",
    "cd /home/dco1020/openairinterface5g/cmake_targets/ran_build/build/\n",
    "\n",
    "sudo RFSIMULATOR=server ./nr-softmodem --rfsim --sa -O ../../../targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb.sa.band78.fr1.106PRB.usrpb210.conf --gNBs.[0].min_rxtxtime 6 --continuous-tx\n",
    "\n",
    "***Step 2.2:*** Deployment of UE:\n",
    "\n",
    "To deploy the UE, type in a new Linux terminal:\n",
    "\n",
    "cd /home/dco1020/openairinterface5g/cmake_targets/ran_build/build/\n",
    "\n",
    "sudo RFSIMULATOR=192.168.70.129 ./nr-uesoftmodem -r 106 --numerology 1 --band 78 -C 3619200000 --rfsim --sa --nokrnmod -O ../../../targets/PROJECTS/GENERIC-NR-5GC/CONF/ue.conf --uicc0.imsi 001010000000002\n",
    "\n",
    "**Attention:** Note that in the deployment command, the parameter \"--uicc0.imsi 001010000000002\" has been added, referring to the IMSI of the second UE, you MUST enter an IMSI that is present in the database (as shown in the \"UE Configuration\" section) and that IS NOT BEING USED BY ANOTHER UE."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
